<template>
  <div>
    <!--条件筛选-->
    <TopFilter :list="topFilterList" :cols="3" labelWidth="110" :collapse="boolean" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <!-- 列筛选-->
    <FilterTable
      ref="table"
      columnsRef="whm"
      :columns="columns"
      :dataSource="tableDataList"
      :isMemoryPagination="true"
      :isExportExcel="true"
      :pageSize="20"
      exportFileName="流失客户再回厂.xls"
      :isSelectColumn="isSelectColumn"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChangeHandle"
    >
      <template slot="controls">
        <el-button type="primary" @click="BtnClick">汇总</el-button>
        <!-- <el-button type="primary" @click="historyFun">维修历史</el-button> -->
      </template>
    </FilterTable>
    <BasePrint ref="print" :data="printList" template="businessOrder" />
    <Drawer :visible.sync="visible" title="汇总" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <Collect :rowData="formData" @close="closeCollect" />
    </Drawer>
  </div>
</template>

<script>
/**
 * 查询流失客户 流失再回厂
 */
import { dictionary } from '@/utils/dictMixin';
import { isFormEmpty } from '@/utils';
import { confirmAction, notifyAction } from '@/utils';
import Collect from './Collect';
import { getList } from '@/api/repair/queryStatistics/queryLoseCustomer/customerChurnBack';
import { getBusinessOrder } from '@/api/repair/queryStatistics/incomeStatistics';
import { getCustomerChurnBack } from '@/api/repair/queryStatistics/queryAccounts';
import moment from 'moment';
import dictCode from '@/utils/dictCode';
import { searchRoleByCode } from '@/api/common/empSelect/index';
import { getWxlxSelect } from '@/api/repair/repairReception/customer';

export default {
  name: 'LHMBusinessOrder',
  components: { Collect },
  mixins: [dictionary],
  data() {
    this.conf = { add: '新增', delete: '删除', edit: '编辑', show: '显示' };
    this.formData = null;
    return {
      topFilterList: this.createTopFilterList(),
      fetchParams: { xhrAbort: true },
      visible: false,
      visible1: false,
      boolean: false, // EP-25572
      isSelectColumn: false,
      printList: [],
      tableDataList: [],
      dialogTitle: '',
      fetchApi: getList,
      columns: this.createTableCheckColumns(),
      handleType: '',
      formData: {},
      lastYear: '',
      lastYearEnd: '',
      loseYear: '',
      loseYearEnd: '',
      tableData: [],
      ids: []
    };
  },
  mounted() {
    // 分页
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.getWxtypeSelect();
    this.init();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'RANGE_DATE',
          label: '流失前进厂日期',
          fieldName: 'beginDate_ls|endDate_ls',
          placeholder: '选择日期',
          change: val => {
            console.log(666, val);
            if (val[1]) {
              // 流失开始日期等于流失前进场日期+1
              this.loseYear = moment(val[1])
                .add(1, 'days')
                .format('YYYY-MM-DD HH:mm:ss');
              this.loseYearEnd = '';
              this.topFilterList.find(x => x.fieldName === 'beginDate_l|endDate_l').initialValue = [this.loseYear, undefined];
            } else {
              console.log('缺时间');
            }
          }
        },
        {
          type: 'RANGE_DATE',
          label: '流失日期',
          fieldName: 'beginDate_l|endDate_l',
          placeholder: '选择日期',
          change: val => {
            console.log('思乡的心绪流失日期', val);
            if (val[1]) {
              // 最后进场开始日期等于流失日期+1
              this.lastYear = moment(val[1])
                .add(1, 'days')
                .format('YYYY-MM-DD HH:mm:ss');
              this.topFilterList.find(x => x.fieldName === 'beginDate_z|endDate_z').initialValue = [this.lastYear, undefined];
            }
          }
        },
        {
          type: 'RANGE_DATE',
          label: '最后进厂日期',
          fieldName: 'beginDate_z|endDate_z',
          placeholder: '选择日期'
        }
      ];
    },
    createTableCheckColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '首次进厂日期',
          dataIndex: 'FIRST_IN_DATE',
          width: 150,
          sorter: true,
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '最后进厂日期',
          dataIndex: 'LAST_MAINTAIN_DATE',
          width: 150,
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '客户代码',
          dataIndex: 'OWNER_NO',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户名称',
          dataIndex: 'OWNER_NAME',
          showOverflowTooltip: true,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '证件号码',
          dataIndex: 'CT_CODE',
          showOverflowTooltip: true,
          sorter: true,
          filter: true,
          filterType: 'input',
          filterItems: this.createDictList('8013'),
          dictItems: this.createDictList('8013'),
          render: props => {
            return <span>{this.createDictText(props.row.ENGINE_NO, '8013')}</span>;
          }
        },
        {
          title: '车型代码',
          dataIndex: 'MODEL_NAME',
          showOverflowTooltip: true,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '底盘号',
          dataIndex: 'VIN',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发动机号',
          dataIndex: 'ENGINE_NO',
          showOverflowTooltip: true,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '行驶里程',
          dataIndex: 'MILEAGE',
          showOverflowTooltip: true,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '牌照号',
          dataIndex: 'LICENSE',
          showOverflowTooltip: true,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '联系人姓名',
          dataIndex: 'CONTACTOR_NAME',
          showOverflowTooltip: true,
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '联系人电话',
          dataIndex: 'CONTACTOR_MOBILE',
          showOverflowTooltip: true,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车系代码',
          dataIndex: 'SERIES_NAME',
          showOverflowTooltip: true,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '送修人姓名',
          dataIndex: 'DELIVERER',
          showOverflowTooltip: true,
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '送修人电话',
          dataIndex: 'DELIVERER_MOBILE',
          showOverflowTooltip: true,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '本公司售车',
          dataIndex: 'IS_SELF_COMPANY',
          showOverflowTooltip: true,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销售单编号',
          showOverflowTooltip: true,
          dataIndex: 'SALES_NO',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '分期付款标识',
          showOverflowTooltip: true,
          dataIndex: 'INSTALL_INDENT',
          sorter: true,
          filter: true,
          defaultEditable: true
        },
        {
          title: '建档日期',
          dataIndex: 'CREATED_AT',
          showOverflowTooltip: true,
          sorter: true,
          sorter: true,
          filter: true,
          dateFormat: 'yyyy-MM-dd',
          showOverflowTooltip: true,
          filterType: 'date-range'
        },
        {
          title: '客户地址',
          dataIndex: 'ADDRESS',
          showOverflowTooltip: true,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '邮编',
          dataIndex: 'ZIP_CODE',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '上次保养日期',
          dataIndex: 'LAST_MAINTENANCE_DATE',
          showOverflowTooltip: true,
          sorter: true,
          sorter: true,
          filter: true,
          dateFormat: 'yyyy-MM-dd',
          showOverflowTooltip: true,
          filterType: 'date-range'
        },
        {
          title: '售车单位',
          showOverflowTooltip: true,
          dataIndex: 'SALES_AGENT_NAME',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '日平均行驶里程',
          showOverflowTooltip: true,
          dataIndex: 'DAILY_AVERAGE_MILEAGE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '省',
          dataIndex: 'PROVINCE_NAME',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '市',
          dataIndex: 'CITY_NAME',
          showOverflowTooltip: true,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '县',
          dataIndex: 'DISTRICT_NAME',
          showOverflowTooltip: true,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '备注',
          dataIndex: 'REMARK',
          showOverflowTooltip: true,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '购车日期',
          dataIndex: 'SALES_DATE',
          showOverflowTooltip: true,
          sorter: true,
          sorter: true,
          filter: true,
          dateFormat: 'yyyy-MM-dd',
          showOverflowTooltip: true,
          filterType: 'date-range'
        },
        {
          title: '车辆类别',
          dataIndex: 'VEHICLE_CATEGORY',
          showOverflowTooltip: true,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '首保日期',
          dataIndex: 'FIRST_MAINTAIN_DATE',
          showOverflowTooltip: true,
          sorter: true,
          sorter: true,
          filter: true,
          dateFormat: 'yyyy-MM-dd',
          showOverflowTooltip: true,
          filterType: 'date-range'
        },
        {
          title: '最后接待服务顾问',
          dataIndex: 'LAST_CONSULTANT_ID',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'checkbox',
          filterItems: [],
          editType: 'select',
          editItems: []
        },
        {
          title: '维修类别',
          dataIndex: 'REPAIR_TYPE_CODE',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        }
      ];
    },
    BtnClick() {
      this.visible = true;
    },
    closeCollect() {
      this.visible = false;
    },
    selectChangeHandle(val) {
      // 行选中状态变化
      this.selectedRow = val;
    },
    async getWxtypeSelect() {
      const res = await getWxlxSelect();
      // console.log('维修项目==============维修类型' + res);
      this.columns.find(x => x.dataIndex === 'REPAIR_TYPE_CODE').editItems = res.data.map(x => ({ text: x.REPAIR_TYPE_NAME, value: x.REPAIR_TYPE_CODE + '' }));
    },

    async init() {
      let obj = { role: dictCode.SERVICE_CONSULTANT };
      const res = await searchRoleByCode(obj);
      const filterData = res.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
      console.log('filterData:', filterData);
      this.columns.find(x => x.dataIndex === 'LAST_CONSULTANT_ID').filterItems = this.columns.find(x => x.dataIndex === 'LAST_CONSULTANT_ID').editItems = filterData;
    },

    async changeHandle(val) {
      this.$refs.table.START_LOADING();
      // 流失日期 = 流失前进厂日期+1
      let YearChange = moment(val.endDate_ls)
        .add(1, 'days')
        .format('YYYY-MM-DD HH:mm:ss');
      // 最后进场日期 = 流失日期 + 1
      let YearChange1 = moment(val.endDate_l)
        .add(1, 'days')
        .format('YYYY-MM-DD HH:mm:ss');
      // 非空
      if (isFormEmpty(val.beginDate_ls) || isFormEmpty(val.endDate_ls) || isFormEmpty(val.beginDate_l) || isFormEmpty(val.endDate_l) || isFormEmpty(val.beginDate_z) || isFormEmpty(val.endDate_z)) {
        notifyAction('请检查日期格式', 'warning');
      } else {
        // 判断日期格式
        if (val.beginDate_l === YearChange && val.beginDate_z === YearChange1) {
          this.fetchParams = Object.assign({}, val);
          this.formData = this.fetchParams; // EP-25572
          const res = await getList(this.formData);
          this.tableDataList = res.data;
        } else {
          notifyAction('请检查日期格式', 'warning');
          this.$refs.table.STOP_LOADING();
        }
      }
      this.$refs.table.STOP_LOADING();
      // this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    saveHandle() {
      console.log('事件待定');
    }
  }
};
</script>
<style lang="less" scoped></style>
